#!/bin/bash

# this file transfer the mapped documentId to originalId

if [ $# -ne 3 ];
then
    echo "usage: transferId.sh src_file dst_file docMapDict";
    exit
fi

srcFile=$1
dstFile=$2

documentIdMapFile=$3
#"../data/lsa_full/document_id.map"
awk '
FILENAME==ARGV[1]{
    { #second use the dict file to get the select user dict
        selectDocument[$2] = $1;
    }
}
FILENAME==ARGV[2]{
    #select and map the ratings
    documentId = selectDocument[$1];
    idLength=split($2, tmpArray, ",");
    printf("%s ", documentId);
    for (dId=1; dId<=idLength;  ++dId) {
        if (length(tmpArray[dId]) < 1) continue;
        split(tmpArray[dId], termArray, "@");
        original_dId = selectDocument[termArray[1]];
        if (idLength == dId) {
            printf("%s@%.4f", original_dId, termArray[2]);
        }
        else {
            printf("%s@%.4f,", original_dId, termArray[2]);
        }
    }
    printf("\n");
}' $documentIdMapFile $srcFile > $dstFile
